package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    public OrderSettingService orderSettingService;

    @PostMapping("/saveOrEdit")
    public Result saveOrEdit(@RequestBody OrderSetting orderSetting) {
        orderSettingService.saveOrEdit(orderSetting);
        return new Result(true, "基于日历修改预约设置成功!");
    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        try{
            List<Map> orderSettingByMonth = orderSettingService.getOrderSettingByMonth(month);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettingByMonth);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        List<String[]> strings = null;
        try {
            strings = POIUtils.readExcel(excelFile);
            Result result = orderSettingService.upload(strings);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }
}
